#
#  Copyright (c) 2021 Futurewei Technologies, Inc.
#
#  clang2mpl is licensed under Mulan PSL v2.
#  You can use this software according to the terms and conditions of the Mulan
#  PSL v2. You may obtain a copy of Mulan PSL v2 at:
#
#      http://license.coscl.org.cn/MulanPSL2
#
#  THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY
#  KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
#  NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. See the
#  Mulan PSL v2 for more details.
#
cmake_minimum_required(VERSION 3.10)
project(clang2mpl)

set( LLVM_LINK_COMPONENTS
  ${LLVM_TARGETS_TO_BUILD}
  Option
  Support
  )

find_package(OpenSSL REQUIRED)
find_package(Threads)

include_directories( $ENV{MAPLE_ROOT}/src/mapleall/maple_ir/include
  $ENV{MAPLE_ROOT}/src/mapleall/maple_util/include
  $ENV{MAPLE_ROOT}/src/mapleall/maple_ipa/include
  $ENV{MAPLE_ROOT}/src/mapleall/maple_phase/include
  $ENV{MAPLE_ROOT}/src/mapleall/maple_driver/include
  $ENV{MAPLE_ROOT}/src/mapleall/maple_me/include
  $ENV{MAPLE_ROOT}/src/mapleall/mpl2mpl/include
  $ENV{MAPLE_ROOT}/src/mapleall/mempool/include
  $ENV{MAPLE_ROOT}/third_party/bounds_checking_function/include
  $ENV{MAPLE_ROOT}/tools/clang+llvm-12.0.0-x86_64-linux-gnu-ubuntu-18.04/include
  ${OPENSSL_INCLUDE_DIR}
  )

link_directories( ${CMAKE_INSTALL_PREFIX}/lib/64
  $ENV{MAPLE_ROOT}/tools/clang+llvm-12.0.0-x86_64-linux-gnu-ubuntu-18.04/lib
)
SET(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)

set(CMAKE_C_COMPILER $ENV{MAPLE_ROOT}/tools/bin/clang)
set(CMAKE_CXX_COMPILER $ENV{MAPLE_ROOT}/tools/bin/clang++)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-rtti -std=c++17")

add_executable(clang2mpl
  Clang2Maple.cpp
  Clang2MapleResult.cpp
  Clang2MapleVisitor.cpp
  )

target_compile_definitions(clang2mpl
  PRIVATE DYNAMICLANG
  PRIVATE MAPLE_ROOT=\"$ENV{MAPLE_ROOT}\"
  PRIVATE INSTALL_DIR=\"${CMAKE_INSTALL_PREFIX}\")
if (CMAKE_BUILD_TYPE STREQUAL "Debug")
  target_compile_definitions(clang2mpl PRIVATE DEBUG)
endif (CMAKE_BUILD_TYPE STREQUAL "Debug")

target_link_libraries(clang2mpl
  PRIVATE
  $ENV{MAPLE_ROOT}/tools/clang+llvm-12.0.0-x86_64-linux-gnu-ubuntu-18.04/lib/libclangTooling.a
  $ENV{MAPLE_ROOT}/tools/clang+llvm-12.0.0-x86_64-linux-gnu-ubuntu-18.04/lib/libclang-cpp.so
  $ENV{MAPLE_ROOT}/tools/clang+llvm-12.0.0-x86_64-linux-gnu-ubuntu-18.04/lib/libclangAST.a
  $ENV{MAPLE_ROOT}/tools/clang+llvm-12.0.0-x86_64-linux-gnu-ubuntu-18.04/lib/libclangBasic.a
  $ENV{MAPLE_ROOT}/tools/clang+llvm-12.0.0-x86_64-linux-gnu-ubuntu-18.04/lib/libclangDriver.a
  $ENV{MAPLE_ROOT}/tools/clang+llvm-12.0.0-x86_64-linux-gnu-ubuntu-18.04/lib/libclangFrontend.a
  $ENV{MAPLE_ROOT}/tools/clang+llvm-12.0.0-x86_64-linux-gnu-ubuntu-18.04/lib/libclangRewriteFrontend.a
  $ENV{MAPLE_ROOT}/tools/clang+llvm-12.0.0-x86_64-linux-gnu-ubuntu-18.04/lib/libclangSerialization.a
  $ENV{MAPLE_ROOT}/tools/clang+llvm-12.0.0-x86_64-linux-gnu-ubuntu-18.04/lib/libclangStaticAnalyzerFrontend.a
  $ENV{MAPLE_ROOT}/tools/clang+llvm-12.0.0-x86_64-linux-gnu-ubuntu-18.04/lib/libclangToolingSyntax.a
  -Wl,--start-group
  ${CMAKE_INSTALL_PREFIX}/lib/64/libmplir.a
  ${CMAKE_INSTALL_PREFIX}/lib/64/libdriver_option.a
  ${CMAKE_INSTALL_PREFIX}/lib/64/liboption_parser.a
  ${CMAKE_INSTALL_PREFIX}/lib/64/libmempool.a
  ${CMAKE_INSTALL_PREFIX}/lib/64/libmpl2mpl.a
  ${CMAKE_INSTALL_PREFIX}/lib/64/libmplutil.a
  -Wl,--end-group
  ${CMAKE_INSTALL_PREFIX}/lib/64/libHWSecureC.a
  ${CMAKE_THREAD_LIBS_INIT}
  )

install(
  TARGETS clang2mpl
  RUNTIME DESTINATION bin)

set(header_install_dir lib/clang2mpl/include)

install(
  DIRECTORY ${CMAKE_SOURCE_DIR}/sys/include/
  DESTINATION ${header_install_dir})
